﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsynchronousTask
{
    public class TrivialProvider
    {
        public String LongTask(int waitPeriod)
        {
            Thread.Sleep(waitPeriod);
            return "LongTask";
        }

        public void LongTaskAsync(int waitPeriod, Action<String> action)
        {
            SynchronizationContext syncContext = SynchronizationContext.Current;

            Task.Factory.StartNew(() =>
                {
                    String s = LongTask(waitPeriod);

                    if (syncContext == null)
                    {
                        action(s);
                    }
                    else
                    {
                        syncContext.Post(state => { action((String)state); }, s);
                    }
                });
        }
    }
}
